package com.sdeonline.bible.creencias;

import java.util.ArrayList;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BeliefsPagerAdapter extends PagerAdapter {
    
	private Context cxt;
	
	BeliefsPagerAdapter(Context conxt) {
		cxt = conxt;
	}
	
	@Override
    public int getCount() {
            return 34;
    }

	/**
	 * Create the page for the given position.  The adapter is responsible
	 * for adding the view to the container given here, although it only
	 * must ensure this is done by the time it returns from
	 * {@link #finishUpdate()}.
	 *
	 * @param container The containing View in which the page will be shown.
	 * @param position The page position to be instantiated.
	 * @return Returns an Object representing the new page.  This does not
	 * need to be a View, but can be some other container of the page.
	 */
	@Override
	public Object instantiateItem(View collection, int position) {
			ListView lv = new ListView(cxt);
			ArrayList<String> references = new ArrayList<String>();
			ArrayAdapter<String> referencesAdapter = new ArrayAdapter<String>(cxt, R.layout.beliefs_list_item, references) {
				  @Override
				  public boolean isEnabled(int position) 
				  {
					  return (position < 2)? false: true;
				  }
			};
			lv.setAdapter(referencesAdapter);
			
			references.add(cxt.getResources().getStringArray(R.array.beliefs_titles_array)[position]);
			references.add(cxt.getResources().getStringArray(R.array.beliefs_descriptions_array)[position]);
			Integer mArrayId = cxt.getResources().getIdentifier("belief_references_"+position, "array", cxt.getPackageName());
			if (mArrayId > 0) 
				for (String mBeliefTxtReference : cxt.getResources().getStringArray(mArrayId)) {
					references.add(mBeliefTxtReference);
				}
            referencesAdapter.notifyDataSetChanged();
			
	        ((ViewPager) collection).addView(lv,0);
	        
	        return lv;
	}

    /**
     * Remove a page for the given position.  The adapter is responsible
     * for removing the view from its container, although it only must ensure
     * this is done by the time it returns from {@link #finishUpdate()}.
     *
     * @param container The containing View from which the page will be removed.
     * @param position The page position to be removed.
     * @param object The same object that was returned by
     * {@link #instantiateItem(View, int)}.
     */
    @Override
    public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((ListView) view);
    }

    
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
            return view==((ListView)object);
    }

        
    /**
     * Called when the a change in the shown pages has been completed.  At this
     * point you must ensure that all of the pages have actually been added or
     * removed from the container as appropriate.
     * @param container The containing View which is displaying this adapter's
     * page views.
     */
    @Override
    public void finishUpdate(View arg0) {}
    

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
            return null;
    }

    @Override
    public void startUpdate(View arg0) {}
    
}

